package util;

import java.net.ConnectException;
import java.sql.*;

public class DBUtil {
    private static final String URL="jdbc:mysql://localhost:3306/student_gui?useUnicode=true&ue&useSSL=false";
    private static final String DRIVER="com.mysql.cj.jdbc.Driver";
    private static final String USER_NAME="root";
    private static final String PWD="111111";
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConn(){
        try {
           return DriverManager.getConnection(URL,USER_NAME,PWD);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    public static void closeConn(Connection connection){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void closePs(PreparedStatement ps){
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void closeRs(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
